package com.hdyg.sixg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.BuildConfig;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.AppManager;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.LoginBean;
import com.hdyg.sixg.entry.VersionBean;
import com.hdyg.sixg.mvp.Contrant.CLogin;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PLogin;
import com.hdyg.sixg.util.LangUtil.MultiLanguageType;
import com.hdyg.sixg.util.LangUtil.MultiLanguageUtil;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.SPUtils;
import com.hdyg.sixg.util.VersionUtil.AppDownloadManager;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements CLogin.IVLogin {

    private final static int REQUEST_CODE = 0x00001;

    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.bt_regist)
    Button btRegist;
    @BindView(R.id.bt_forget_pwd)
    Button btForgetPwd;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.tv_login_face)
    TextView tvLoginFace;
    @BindView(R.id.tv_lang_title)
    TextView tvLangTitle;
    @BindView(R.id.ll_lang)
    LinearLayout llLang;
    @BindView(R.id.iv_lang)
    ImageView ivLang;


    private String langType;
    private boolean need = false;
    private CLogin.IPLogin mPresenter;
    private String userName, userPwd, versionName;
    private AppDownloadManager mDownLoadManage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        langType = SPUtils.get(SpMsg.LANG, MultiLanguageType.ZH.typeKey);
        mDownLoadManage = new AppDownloadManager(this);
        if (langType.equals(MultiLanguageType.ZH.typeKey)) {
            tvLangTitle.setText(Html.fromHtml("<u>" + getResources().getString(R.string.lang_zh) + "</u>"));
            ivLang.setImageResource(R.mipmap.lang_china);
        } else {
            tvLangTitle.setText(Html.fromHtml("<u>" + getResources().getString(R.string.lang_en) + "</u>"));
            ivLang.setImageResource(R.mipmap.lang_english);
        }
        etAccount.setText(SPUtils.get(SpMsg.USERNAME, ""));
        etPwd.setText(SPUtils.get(SpMsg.USERPASSWORD, ""));
        versionName = BuildConfig.VERSION_NAME;
    }


    @Override
    protected void initData() {
        mPresenter.pGetVersion(BaseUrl.VERSION_URL, GetParamUtil.getVersionParam());
    }

    private void showLangPop(View v) {

        View view = View.inflate(mContext, R.layout.pop_lang, null);
        LinearLayout llZh = view.findViewById(R.id.ll_zh);
        LinearLayout llEn = view.findViewById(R.id.ll_en);
        llZh.setOnClickListener(v1 -> {
            tvLangTitle.setText(Html.fromHtml("<u>" + getResources().getString(R.string.lang_zh) + "</u>"));
            ivLang.setImageResource(R.mipmap.lang_china);
            mPopupWindow.dismiss();
            need = MultiLanguageUtil.needUpdateLocale(mContext, MultiLanguageType.ZH);
            transLanguage(need, MultiLanguageType.ZH);
        });
        llEn.setOnClickListener(v1 -> {
            tvLangTitle.setText(Html.fromHtml("<u>" + getResources().getString(R.string.lang_en) + "</u>"));
            ivLang.setImageResource(R.mipmap.lang_english);
            mPopupWindow.dismiss();
            need = MultiLanguageUtil.needUpdateLocale(mContext, MultiLanguageType.EN);
            transLanguage(need, MultiLanguageType.EN);
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.WRAP_CONTENT)
                .showAsDropDown(mContext, v, 0);
    }

    private void transLanguage(boolean need, MultiLanguageType type) {
        if (need) {
            //自己写的常用activity管理工具
            MultiLanguageUtil.updateLanguageEnviroment(mContext, type);
            AppManager.getAppManager().recreateAllOtherActivity(this);
            finish();
            startActivity(LoginActivity.class);
        } else {
//            toastNotifyShort("不需要切换");
        }
        mPopupWindow.dismiss();
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PLogin(this);
    }

    @OnClick({R.id.bt_regist, R.id.bt_forget_pwd, R.id.bt_login, R.id.tv_login_face, R.id.ll_lang})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_regist:
                mBundle = new Bundle();
                mBundle.putInt(SpMsg.INTENT_CODE, RegistForgetPwdActivity.REGIST_CODE);
                startActivityForResult(RegistForgetPwdActivity.class, mBundle, REQUEST_CODE);
                break;
            case R.id.bt_forget_pwd:
                mBundle = new Bundle();
                mBundle.putInt(SpMsg.INTENT_CODE, RegistForgetPwdActivity.FORGET_PWD_CODE);
                startActivityForResult(RegistForgetPwdActivity.class, mBundle, REQUEST_CODE);
                break;
            case R.id.bt_login:

                userName = etAccount.getText().toString().trim();
                userPwd = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(userName)) {
                    toastNotifyShort(R.string.login_account_hint);
                    return;
                }
                if (TextUtils.isEmpty(userPwd)) {
                    toastNotifyShort(R.string.login_pwd_hint);
                    return;
                }
                mPresenter.pGetLogin(BaseUrl.LOGIN_URL, GetParamUtil.getLoginParam(userName, userPwd));
                break;
            case R.id.tv_login_face://点击了人脸识别登录

                break;
            case R.id.ll_lang:
                showLangPop(v);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                etAccount.setText(SPUtils.get(SpMsg.USERNAME, ""));
                etPwd.setText(SPUtils.get(SpMsg.USERPASSWORD, ""));
                break;
        }
    }

    @Override
    public void vGetLoginSuccess(LoginBean dataBean) {
        SPUtils.put(SpMsg.TOKEN, dataBean.getToken());
        SPUtils.put(SpMsg.USERNAME, userName);
        SPUtils.put(SpMsg.USERPASSWORD, userPwd);
        startActivity(MainActivity.class);
    }

    @Override
    public void vGetVersionSuccess(VersionBean dataBean) {
        if (!versionName.equals(dataBean.getVersion())) {
            mDownLoadManage.showNoticeDialog(dataBean.getUrl(), getResources().getString(R.string.sys_version_title2), dataBean.getNote());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mDownLoadManage != null) {
            mDownLoadManage.resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mDownLoadManage != null) {
            mDownLoadManage.onPause();
        }
    }
}
